package com.big.pay.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置消息队列。
 * 包含订单队列
 * 支付死信队列
 */
@Configuration
public class RabbitConfig {

    /**
     * 短信
     */
    public static final String EXCHANGE_DIRECT_MSM = "exchange.direct.msm";

    public static final String ROUTING_MSM_ITEM = "msm.item";
    //队列
    public static final String QUEUE_MSM_ITEM  = "queue.msm.item";

    /**
     * 支付
     */
    //支付交换机
    public static final String EXCHANGE_DIRECT_PAY = "pay.direct";
    // ROUTING_KEY
    public static final String ROUTING_PAY_ITEM = "pay.routing";
    //队列
    public static final String QUEUE_PAY_ITEM  = "pay.item";

    /**
     * 订单状态
     */
    //订单交换机
    public static final String EXCHANGE_DIRECT_ORDER_STATUS = "order.status.direct";
    // ROUTING_KEY
    public static final String ROUTING_ORDER_STATUS_ITEM = "order.status.routing";
    //队列
    public static final String QUEUE_ORDER_STATUS_ITEM  = "order.status.item";


    /**
     * 库存仓库
     */
    //支付交换机
    public static final String EXCHANGE_DIRECT_STOCK = "stock.direct";
    // ROUTING_KEY
    public static final String ROUTING_STOCK_ITEM = "stock.routing";
    //队列
    public static final String QUEUE_STOCK_ITEM  = "stock.item";



    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }



    /**
     * 支付队列
     */
    @Bean
    public Queue PayQueue(){
        Map<String,Object> args = new HashMap<>();
        Queue sms = new Queue(QUEUE_PAY_ITEM,true,false,false, args);
        return  sms;
    }


    /**
     * 支付交换器
     * @return
     */
    @Bean
    public Exchange PayExchange(){
        Map<String, Object> args = new HashMap<>();
        DirectExchange exchange = new DirectExchange(EXCHANGE_DIRECT_PAY, true, false, args);
        return exchange;
    }


    /**
     * 支付队列绑定支付交换机
     */
    @Bean
    public Binding PayQueueBindingPayExchange(){
        return BindingBuilder.bind(PayQueue()).to(PayExchange()).with(ROUTING_PAY_ITEM).noargs();
    }



    /**
     * 订单队列
     */
    @Bean
    public Queue OrderStatusQueue(){
        Queue sms = new Queue(QUEUE_ORDER_STATUS_ITEM,true,false,false, null);
        return  sms;
    }


    /**
     * 订单交换器
     * @return
     */
    @Bean
    public Exchange OrderStatusExchange(){
        Map<String, Object> args = new HashMap<>();
        DirectExchange exchange = new DirectExchange(EXCHANGE_DIRECT_ORDER_STATUS, true, false, args);
        return exchange;
    }


    /**
     * 订单队列绑定订单交换机
     */
    @Bean
    public Binding OrderStatusQueueBindingOrderStatusExchange(){
        return BindingBuilder.bind(OrderStatusQueue()).to(OrderStatusExchange()).with(ROUTING_ORDER_STATUS_ITEM).noargs();
    }



    /**
     * 库存队列
     */
    @Bean
    public Queue StockQueue(){
        Map<String,Object> args = new HashMap<>();
        Queue sms = new Queue(QUEUE_STOCK_ITEM,true,false,false, args);
        return  sms;
    }


    /**
     * 库存交换器
     * @return
     */
    @Bean
    public Exchange StockExchange(){
        Map<String, Object> args = new HashMap<>();
        DirectExchange exchange = new DirectExchange(EXCHANGE_DIRECT_STOCK, true, false, args);
        return exchange;
    }


    /**
     * 库存队列绑定库存交换机
     */
    @Bean
    public Binding StockQueueBindingStockExchange(){
        return BindingBuilder.bind(StockQueue()).to(StockExchange()).with(ROUTING_STOCK_ITEM).noargs();
    }


    /**
     * 短信队列
     */
    @Bean
    public Queue SmsQueue(){
        Map<String,Object> args = new HashMap<>();
        Queue sms = new Queue(QUEUE_MSM_ITEM,true,false,false, args);
        return  sms;
    }
    /**
     * 短信交换器
     * @return
     */
    @Bean
    public Exchange SmsExchange(){
        Map<String, Object> args = new HashMap<>();
        DirectExchange exchange = new DirectExchange(EXCHANGE_DIRECT_MSM, true, false, args);

        return exchange;
    }
    /**
     * 用于短信
     */
    @Bean
    public Binding SmsBinding(){
        return BindingBuilder.bind(SmsQueue()).to(SmsExchange()).with(ROUTING_MSM_ITEM).noargs();

    }


}
